<?php
/**
 * 车展基金预算
 * @author qianxun
 *
 */
class CarBudget extends MY_Controller{
    function CarBudget(){
        parent::MY_Controller();
    }
    /**
     * 专营店列表
     */
    function storeList(){
        if ($this->user['userRole'] != 1){
            showError('您无权限进去此页面');
        }
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>'','title'=>'车展基金预算申请列表');
        
        //载入模型
        $this->load->model('carProcessModel');
        //年份
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');;
        $this->viewData['year'] = $year;
        //时间单位配置
        $this->viewData['timeUnitList'] = $this->timeUnitModel->getTimeUnitList($year);
        $where = array();
        //专营店ID条件
        $where['storeId'] = array('in', $this->userLib->getViewStoreIds());
        //年份条件
        $where['year'] = $year;
        //车展基金列表
        $procList = $this->carProcessModel->getNewListByTimeUnit($where);
        $this->viewData['procList'] = $procList;
        $this->load->view('CarBudget/storeList', $this->viewData);
    }
    /**
     * 计划申请
     */
    function storeAdd(){
        //年
        $data['year']   = $this->input->get('year');
        $data['year'] || showError('请选择年份');
        //月
        $data['timeUnit']   = $this->input->get('timeUnit');
        $data['timeUnit'] || showError('请选择时段');
        //专营店ID
        $data['storeId']    = $this->user['userId'];
        if (!$this->auth->isCarBudgetAdd($data)){
            showError('您无权限申报,请确认已开通申报权限');
        }
        //处理数据
        $this->load->model('carProcessModel');
        $procId = $this->carProcessModel->initData($data);
        redirect(printUrl('CarBudget', 'storeEdit', array('procId'=>$procId)));
    }
    /**
     * 计划修改
     */
    function storeEdit(){
        $procId = (int)$this->input->get('procId');
        $procId || showError('请选择要修改的双月数据');
        $this->viewData['procId'] = $procId;
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('CarBudget', 'storeList'),'title'=>'车展基金预算申请列表');
        $this->viewData['breadcrumb'][] = array('url'=>'','title'=>'车展基金预算编辑');
        //主线流程数据
        $this->load->model('carProcessModel');
        $procData = $this->carProcessModel->getNewData(array('id'=>$procId,'storeId'=>$this->user['userId']));
        $this->viewData['procData'] = $procData;
        if (!$this->auth->isCarBudgetEdit($procData) ){
            showError('您无权限修改');
        }
        //专营店数据
        $this->load->model('sellpointModel');
        $this->viewData['storeData'] = $this->sellpointModel->getStoreIdData($procData['storeId']);;
        //项目列表
        $this->load->model('carProjectModel');
        $this->viewData['projectList'] = $this->carProjectModel->getNewList(array('procId'=>$procId,'isBubao'=>0), NULL, NULL, '', 'id desc');
        //审核日志
        $this->load->model('auditLogModel');
        $this->viewData['auditList'] = $this->auditLogModel->getLogs(31, $procId);
        //操作
        $this->viewData['action'] = 'edit';
        $this->viewData['isBubao'] = 0;
        $this->load->view('CarBudget/storeEdit', $this->viewData);
    }
    /**
     * 管理端列表
     */
    function adminList(){
        if ($this->user['userRole'] == 1){
            showError('您无权限进去此页面');
        }
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>'','title'=>'车展基金管理');
        //载入模型
        $this->load->model('carProcessModel');
        $this->load->model('sellpointModel');
        //查询条件
        $where = array();
        //专营店筛选
        $area = $this->input->get('area');
        $this->viewData['area'] = $area;
        $region = $this->input->get('region');
        $this->viewData['region'] = $region;
        $spsarea = $this->input->get('spsarea');
        $this->viewData['spsarea'] = $spsarea;
        $province = $this->input->get('province');
        $this->viewData['province'] = $province;
        $city = $this->input->get('city');
        $this->viewData['city'] = $city;
        $storeId = (int)$this->input->get('storeId');
        $this->viewData['storeId'] = $storeId;
        $keyword = $this->input->get('keyword');
        $this->viewData['keyword'] = $keyword;
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($area, $region, $spsarea, $province, $city, $storeId, $keyword);
        $placeStoreIds = array_keys($placeStoreList);
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        if ($viewStoreIds == '*'){
            $where['storeId'] = array('in', $placeStoreIds);
        }else{
            $where['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        //年份
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        //时间单位
        $this->viewData['timeUnit'] = (int)$this->input->get('timeUnit');
        $this->viewData['timeUnit'] && $where['timeUnit'] = $this->viewData['timeUnit'];
        //年份条件
        $where['year'] = $year;
        $where['budgetApply > '] =0;
        $count = $this->carProcessModel->getCount($where);
        //分页
        $this->load->library('pagination');
        $getParam = $_GET;
        unset($getParam['c'], $getParam['m'], $getParam['page']);
        $config['base_url'] = printUrl($_GET['c'], $_GET['m'], $getParam);
        $config['total_rows'] = $count;
        $this->pagination->initialize($config);
        $this->viewData['pager'] = $this->pagination->create_links();
        $page = (int)$this->input->get('page');
        $limit = $this->pagination->per_page;
        
        $this->viewData['procList'] = $this->carProcessModel->getNewList($where, $limit, $page, '', 'id desc');
        $this->load->view('CarBudget/adminList', $this->viewData);
    }
    /**
     * 预算审核
     */
    function adminAudit(){
        $this->load->model('carProcessModel');
        $this->load->model('carProjectModel');
        $this->load->model('auditLogModel');
        if ($_POST){
            $procId = (int)$this->input->post('procId');
            $procId || showError('请选择要审核的双月数据');
            $carshowData = $this->input->post('carshow');//地方车展
            $audit = $this->input->post('audit');//审核数据
            if (!$carshowData){
                showError('您未选择审核项');
            }
            if ($carshowData){
                foreach ($carshowData as $k => $v){
                    $this->carProjectModel->budgetOneKeyAudit((int)$k, (int)$v['budgetAuditRs'], $this->user);
                }
            }
            //插入审核日志
            $this->auditLogModel->addLog(31, array(
                'targetId'=>$procId,
                'auditRole'=>$this->user['userRole'],
                'auditUid' => $this->user['userId'],
                'auditRs' => 1,
                'auditDes' => $audit['auditDes'] ? $audit['auditDes'] : '预算一键审核',
            ));
            showSuccess('审核成功');
        }else{
            $procId = (int)$this->input->get('procId');
            $procId || showError('请选择要审核的双月数据');
            //面包屑
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('CarBudget', 'adminList'),'title'=>'车展基金管理列表');
            $this->viewData['breadcrumb'][] = array('url'=>'','title'=>'车展基金预算审核');
            //主线流程数据
            $procData = $this->carProcessModel->getNewData(array('id'=>$procId));
            $procData || showError('双月数据不存在');
            $this->viewData['procData'] = $procData;
            if (!$this->auth->isCarBudgetAudit($this->viewData['procData'])){
                showError('您无权限审核');
            }
            //专营店数据
            $this->load->model('sellpointModel');
            $this->viewData['storeData'] = $this->sellpointModel->getStoreIdData($procData['storeId']);;
            //地方车展列表
            $this->viewData['projectList'] = $this->carProjectModel->getNewList(array('procId'=>$procId,'isBubao'=>0,'budgetAuditRs !='=>-1), NULL, NULL, '', 'id desc');
            //审核日志
            $this->viewData['auditList'] = $this->auditLogModel->getLogs(31, $procId);
            //页面动作
            $this->viewData['action'] = 'audit';
            $this->load->view('CarBudget/adminAudit', $this->viewData);
        }
    }
    /**
     * 计划查看
     */
    function check(){
        $procId = (int)$this->input->get('procId');
        $procId || showError('请选择要修改的双月数据');
        $this->viewData['procId'] = $procId;
        //面包屑
        if ($this->user['userRole'] == 1){
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('CarBudget', 'storeList'),'title'=>'车展基金预算申请列表');
        }else{
            $this->viewData['breadcrumb'][] = array('url'=>$_SERVER['HTTP_REFERER'],'title'=>'车展基金管理列表');
        }
        $this->viewData['breadcrumb'][] = array('url'=>'','title'=>'车展基金预算查看');
        //主线流程数据
        $this->load->model('carProcessModel');
        $procData = $this->carProcessModel->getNewData(array('id'=>$procId));
        $this->viewData['procData'] = $procData;
        if (!$this->auth->isCarBudgetCheck($procData) ){
            showError('您无权限查看');
        }
        //专营店数据
        $this->load->model('sellpointModel');
        $this->viewData['storeData'] = $this->sellpointModel->getStoreIdData($procData['storeId']);;
        //项目列表
        $this->load->model('carProjectModel');
        $this->viewData['projectList'] = $this->carProjectModel->getNewList(array('procId'=>$procId,'isBubao'=>0), NULL, NULL, '', 'id desc');
        //审核日志
        $this->load->model('auditLogModel');
        $this->viewData['auditList'] = $this->auditLogModel->getLogs(31, $procId);
        //操作
        $this->viewData['action'] = 'check';
        $this->viewData['isBubao'] = 0;
        $this->load->view('CarBudget/check', $this->viewData);
    }
}